//
//  ScenicDescribeCell.m
//  ImGuider
//
//  Created by llt on 2017/4/18.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "ScenicDescribeCell.h"

@interface ScenicDescribeCell()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIButton *moreBtn;
@property (nonatomic, strong) UIButton * guideLineBtn;
@property (nonatomic) BOOL isClicked;

@end

@implementation ScenicDescribeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = FontBold(17);
        [self addSubview:_titleLabel];
        
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.mas_top).offset(20);
            make.left.equalTo(self.mas_left).offset(15);
            make.right.equalTo(self.mas_right).offset(-45);
            make.height.mas_equalTo(20);
        }];
        
        _guideLineBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [self addSubview:_guideLineBtn];
        
        [_guideLineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_titleLabel.mas_centerY);
            make.right.equalTo(self.mas_right).offset(-10);
            make.width.mas_equalTo(30);
            make.height.mas_equalTo(30);
        }];
        
        [_guideLineBtn setImage:[UIImage imageNamed:@"lines_gps"] forState:UIControlStateNormal];
        [_guideLineBtn addTarget:self action:@selector(guideLine:) forControlEvents:UIControlEventTouchUpInside];
        
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = FontBold(16);
        _contentLabel.textColor = [UIColor grayColor];
        [self addSubview:_contentLabel];
        
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom).offset(16);
            make.left.equalTo(self.mas_left).offset(15);
            make.right.equalTo(self.mas_right).offset(-15);
            make.height.mas_equalTo(10);
        }];
        
        _moreBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreBtn addTarget:self action:@selector(moreButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_moreBtn];
        [_moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo (_contentLabel.mas_bottom);
            make.bottom.equalTo(self.mas_bottom);
            make.right.equalTo(self.mas_right);
            make.width.mas_equalTo(50);
        }];
        [_moreBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
        [_moreBtn setImage:[UIImage imageNamed:@"arrowUp"] forState:UIControlStateSelected];
        
    }
    return self;
}

- (void)guideLine:(UIButton *)btn {
    
    if (_delegate && [_delegate respondsToSelector:@selector(guideLine)]) {
        [_delegate guideLine];
    }
}
- (void)setCellContent:(NSString *)contentStr title:(NSString *)title andIsShow:(BOOL)isShow{
    
    
    _titleLabel.text = title;
    _contentLabel.attributedText = [IGUtil attributedStringText:contentStr font:Font(12) color:kContentColor lineSpacing:lineSpace];;
    _moreBtn.selected = isShow;
    
    CGFloat defaultHeight;
    
    // 获取文字高度
    CGFloat height = [IGUtil getHeightWithText:contentStr width:kScreenWidth - 30 lineSpacing:lineSpace font:Font(12) defaultHeight:&defaultHeight];
    if (height > defaultHeight) {
        // 文字大于三行，显示展开收起按钮
        self.moreBtn.hidden = NO;
        if (isShow) {
            _contentLabel.numberOfLines = 0;
            [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make){
                make.left.mas_equalTo(self.contentView.mas_left).offset(15);
                make.top.mas_equalTo(_titleLabel.mas_bottom).offset(16);
                make.width.mas_equalTo(kScreenWidth - 30);
                make.height.mas_equalTo(height + 1);
            }];
        } else {
            [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make){
                make.left.mas_equalTo(self.contentView.mas_left).offset(15);
                make.top.mas_equalTo(_titleLabel.mas_bottom).offset(16);
                make.width.mas_equalTo(kScreenWidth - 30);
                make.height.mas_equalTo(defaultHeight);
            }];
        }
    } else {
        // 文字小于三行，隐藏展开收起按钮
        _contentLabel.numberOfLines = 3;
        self.moreBtn.hidden = YES;
        [_contentLabel mas_remakeConstraints:^(MASConstraintMaker *make){
            make.left.mas_equalTo(self.contentView.mas_left).offset(15);
            make.top.mas_equalTo(_titleLabel.mas_bottom).offset(16);
            make.width.mas_equalTo(kScreenWidth - 30);
            make.height.mas_equalTo(height+1);  // 由于系统计算的那个高度有时候会有1像素到2像素的误差，所以这里把高度+1
        }];
    }
}

+ (CGFloat)cellHeightWith:(NSString *)contentStr andIsShow:(BOOL)isShow {
    
    CGFloat defaultHeight;
    
    CGFloat height = [IGUtil getHeightWithText:contentStr width:kScreenWidth - 30 lineSpacing:lineSpace font:Font(12) defaultHeight:&defaultHeight];
    
    if (height > defaultHeight) {
        if (isShow) {
            return 8 + height + 5 + 20 + 8 + 28+20;
        }else{
            return 8 + defaultHeight + 5 + 20 + 8 + 28+20;
        }
    } else {
        return 8 + height + 8 + 28+20;
    }
}

#pragma mark - 显示更多按钮点击事件
- (void)moreButtonClicked {
    
    self.moreBtn.selected = !self.moreBtn.selected;
    
    if (_delegate) {
        [_delegate describeCell:self isShow:self.moreBtn.selected];
    }
}

@end
